﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Mime;
using UnityEngine;
using UnityEngine.UI;

namespace UGUIFW
{
	public class UIMaskMgr : MonoBehaviour {
        //本脚本私有单例
	    private static UIMaskMgr instance = null;  
        //遮罩面板
	    private GameObject GoMaskPanel;
        //UI摄像机原始的“层深”
	    private float _OriginalUICameralDepth;
        Image mask;
        //得到实例
        public static UIMaskMgr GetInstance()
	    {
	        if (instance==null)
	        {
	            instance = new GameObject("UIMaskMgr").AddComponent<UIMaskMgr>();
	        }
	        return instance;
	    }
        
	    void Awake()
	    {
            //把本脚本实例，作为“脚本节点对象”的子节点。
            UnityHelper.AddChildNodeToParentNode(UIManager.GetInstance().scriptRoot, this.gameObject.transform);
	        GoMaskPanel = UnityHelper.FindTheChildNode(UIManager.GetInstance().UIRoot.gameObject, "UIMaskPanel").gameObject;
            mask = GoMaskPanel.GetComponentInChildren<Image>();
            //得到UI摄像机原始的“层深”
	        if (UIManager.GetInstance().UICamera != null)
	        {
	            //得到UI摄像机原始“层深”
	            _OriginalUICameralDepth = UIManager.GetInstance().UICamera.depth;
	        }
	        else
	        {
	            Debug.Log(GetType()+"/Start()/UI_Camera is Null!,Please Check! ");
            }
	    }

        /// <summary>
        /// 设置遮罩状态
        /// </summary>
        /// <param name="go">需要显示的UI窗体</param>
        /// <param name="lucenyType">显示透明度属性</param>
	    public void SetMaskWindow(GameObject go,EUILucenyType lucenyType=EUILucenyType.Lucency)
        {
            //启用遮罩窗体以及设置透明度
            switch (lucenyType)
            {
                    //完全透明，不能穿透
                case EUILucenyType.Lucency:
                    GoMaskPanel.SetActive(true);
                    Color newColor1 = new Color(0,0,0,0);
                    mask.color = newColor1; 
                    break;
                    //半透明，不能穿透
                case EUILucenyType.Translucency:
                    GoMaskPanel.SetActive(true);
                    Color newColor2 = new Color(0,0,0,0.3f);
                    mask.color = newColor2; 
                    break;
                    //低透明，不能穿透
                case EUILucenyType.ImPenetrable:
                    GoMaskPanel.SetActive(true);
                    Color newColor3 = new Color(0,0,0,0.8f);
                    mask.color = newColor3; 
                    break;
                    //可以穿透
                case EUILucenyType.Pentrate:
                    if (GoMaskPanel.activeInHierarchy)
                    {
                        GoMaskPanel.SetActive(false);
                    }
                    break;

                default:
                    break;
            }
            
            //遮罩窗体下移
            GoMaskPanel.transform.SetAsLastSibling();
            //显示窗体的下移
            go.transform.SetAsLastSibling();
            //增加当前UI摄像机的层深（保证当前摄像机为最前显示）
            if (UIManager.GetInstance().UICamera != null)
            {
                UIManager.GetInstance().UICamera.depth = UIManager.GetInstance().UICamera.depth + 100;    //增加层深
            }
	    }

        /// <summary>
        /// 取消遮罩状态
        /// </summary>
        public void CancelMaskWindow()
	    {
            //禁用遮罩窗体
	        if (GoMaskPanel.activeInHierarchy)
	        {
                //隐藏
	            GoMaskPanel.SetActive(false);
            }

	        //恢复当前UI摄像机的层深 
            if (UIManager.GetInstance().UICamera != null)
            {
                UIManager.GetInstance().UICamera.depth = _OriginalUICameralDepth;  //恢复层深
            }
        }
	}
}